package com.sh.doctors.cloud.patient.viewmodel

import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableBoolean
import androidx.databinding.ObservableField
import com.base.viewmodel.BaseViewModel
import com.helper.extens.async
import com.sh.doctors.cloud.patient.model.local.dao.AriticleDao
import com.sh.doctors.cloud.patient.model.repository.AriticleRepo
import com.sh.doctors.cloud.resp.AriticleBean
import com.sh.doctors.cloud.resp.Result
import io.reactivex.Single
import me.jessyan.autosize.utils.LogUtils


class AriticleModel constructor(private val repo: AriticleRepo, private val local:AriticleDao) : BaseViewModel() {
    val cancel = ObservableField<String>("返回")
    val title = ObservableField<String>("资讯")
    val hasnews = ObservableBoolean(false)
    val error = ObservableField<String>()


    val newsList = ObservableArrayList<AriticleBean>()

    fun  getLocalAriticle(): Single<List<AriticleBean>> = local.getAllArticle().async().doOnSuccess {
       for (bean in it){
           LogUtils.d(bean.toString())
       }
    }

    fun getAriticle():
            Single<Result> = repo.getAriticle().async(1000)
        .doOnSuccess {
            it.data?.news_list?.let { it1 -> newsList.addAll(it1) }
            hasnews.set(true)
        }
        .doOnError {
            hasnews.set(false)
            error.set(it.message)
        }

}

